//
//  GSLoginViewController.m
//  GameSky
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSLoginViewController.h"
#import "GSRegisterViewController.h"
#import "GSGetPasswordViewController.h"


@interface GSLoginViewController ()


@property (nonatomic, strong) UIAlertController *alertController;
@end

@implementation GSLoginViewController
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{

}
#pragma mark---------------视图加载完成------------------
- (void)viewDidLoad {
    [super viewDidLoad];
    // 夜间模式
    self.view.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
//// 定制navigationBar
//    [self creatNavigationBar];
    self.bar.titleLabel.text = @"用户登陆";
    
// 添加子视图
    [self addSubViews];
}
//#pragma mark-------------定制navigationBar--------------
//- (void)creatNavigationBar{
//    // 自定义navigationBar
//    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//    // 返回按钮
//    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back 32*32"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction:)];
//      // 设置 图片的大小
//    [backBarButtonItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
//    self.navigationItem.leftBarButtonItem = backBarButtonItem;
//    // 设置 图片的颜色
//    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
//    self.navigationItem.title = @"用户登录";
//    // 设置字体颜色
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//}
//#pragma mark-------------返回按钮的实现-------------------
//- (void)backAction:(UIBarButtonItem *)item{
////    self.navigationController.navigationBar.hidden = YES;
////    [UIView transitionFromView:self.navigationController.view  toView:self.backView duration:0.3 options:(UIViewAnimationOptionTransitionCurlUp) completion:nil];
//}
#pragma mark-------------添加子视图----------------------
- (void)addSubViews{
    self.loginView = [[GSLoginView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
// 为 注册新账号添加方法
    [_loginView.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
//// 为找回密码 添加方法
//    [_loginView.forgotPasswordButton addTarget:self action:@selector(forgotPasswordButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
// 第三放登陆- QQ
    [_loginView.QQLoginButton addTarget:self action:@selector(QQLoginButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
// 登陆按钮实现
    [_loginView.loginButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [self.view addSubview:_loginView];
}
#pragma mark------------------跳转到注册界面----------------
- (void)registerButtonAction:(UIButton *)button{
    GSRegisterViewController *registerVC = [[GSRegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}
//#pragma mark------------------跳转到找回密码---------------
//- (void)forgotPasswordButtonAction:(UIButton *)button{
//    GSGetPasswordViewController *getPasswordVC = [[GSGetPasswordViewController alloc] init];
//    [self.navigationController pushViewController:getPasswordVC animated:YES];
//}
#pragma mark-----------------登陆按钮实现------------------
- (void)loginButtonAction:(UIButton *)button{
    NSString *userName = _loginView.userNameTextField.text;
    NSString *password = _loginView.passwordTextField.text;
// 发送网络请求
    [self loadLoginDataWithName:userName password:password];
}
- (void)loadLoginDataWithName:(NSString *)name password:(NSString *)password{
// 构建参数字典
    NSDictionary *dic = @{@"request":@{@"userName":name,
                                       @"passWord":password}
                          };
    [GSNetworkManager requestWithUrlStr:kLogin paraDict:dic method:POST success:^(NSURLResponse *response, NSData *data) {
        if (data) {
            // 弹窗
            _alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleAlert)];
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
            NSString *err = dic[@"errorMessage"];
            if (err.length != 0) {
                _alertController
                .message = @"登陆失败";
                UIAlertAction *makeSure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                }];
                [_alertController addAction:makeSure];
                [self presentViewController:_alertController animated:YES completion:nil];
            }else{
            _alertController.message = @"登陆成功";
                [self presentViewController:_alertController animated:YES completion:nil];
                [self performSelector:@selector(loginSeccessed) withObject:nil afterDelay:1];
            }

        }
    } failure:^(NSError *error) {
        
    }];

}
#pragma mark------------------QQ登陆---------------------
- (void)QQLoginButtonAction:(UIButton *)button{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
           
            
            [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToQQ completion:^(UMSocialResponseEntity *response) {
                NSLog(@"SnsInformation is %@",response.data);
                
                
            }];
            
        }
    });

}
#pragma mark-----------视图偏转-----------
- (void)changeTransform{
    [UIView animateWithDuration:0.5 animations:^{
        
        
        self.navigationController.view.transform = CGAffineTransformTranslate(self.navigationController.view.transform, kScreenWidth, 0);
        
    }];
}

#pragma mark------------左侧按钮点击返回---------
- (void)leftButtonClick:(UIButton *)leftButton{
    [self.view endEditing:YES];
    [self changeTransform];
}
#pragma mark------------登陆成功-------------
- (void)loginSeccessed{
    [_alertController dismissViewControllerAnimated:YES completion:nil];
    self.isLoginBlock(YES,_loginView.userNameTextField.text);
    [self changeTransform];
}
#pragma mark---------------------------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
